package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.supplier.LogisticsSelect;
import com.sand.dto.supplier.MerchantS;
import com.sand.dto.supplier.SupplierF;
import com.sand.entity.sell.Debt;
import com.sand.entity.sell.Logistics;
import com.sand.entity.sell.Purchase;
import com.sand.feign.InventoryFeign;
import com.sand.feign.supplierFeign;
import com.sand.mapper.LogisticsMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper, Logistics> implements LogisticsService {

    @Autowired
   private supplierFeign supplierFeign;

    @Autowired
    private InventoryFeign inventoryFeign;

    /**
     * 卖家发货
     *
     * @param supplierF
     */
    @Override
    @Transactional
    public Integer fa(SupplierF supplierF) {

        //参数校验
        panfa(supplierF);

        Purchase data = supplierFeign.gid(supplierF.getPerchaseid()).getData();

        if (data == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该采购单不存在");
        }

        Logistics logistics = new Logistics();

        logistics.setMerchantid(supplierF.getMerchantid());
        logistics.setSupplierid(supplierF.getSupplierid());
        logistics.setPerchaseid(supplierF.getPerchaseid());
        logistics.setFtime(supplierF.getFtime());
        logistics.setFaddr(supplierF.getFaddr());

        //默认货物不到达
        logistics.setArrive(0);
        //默认不取货
        logistics.setFetch(0);
        //默认没有完成物流
        logistics.setWhether(0);

        //添加物流单
        save(logistics);

        //判断采购是否付清
        if (data.getWhether() == 0){
            //添加货款载物
            Debt debt = new Debt();

            debt.setPurchaseid(supplierF.getPerchaseid());
            debt.setMerchantid(supplierF.getMerchantid());
            debt.setSupplierid(supplierF.getSupplierid());
            debt.setMoney(data.getMoney());
            //默认没有还清
            debt.setWhether(0);
            debt.setNomoney(data.getMoney() - data.getSubscription());

            supplierFeign.add(debt);
        }

        return 1;
    }

    /**
     * 条件查看物流
     *
     * @param logisticsSelect
     */
    @Override
    public List<Logistics> all(LogisticsSelect logisticsSelect) {

        LambdaQueryWrapper<Logistics> wrapper = new LambdaQueryWrapper<>();

        if (logisticsSelect.getMerchantid() != null){
            wrapper.eq(Logistics::getMerchantid,logisticsSelect.getMerchantid());
        }

        if (logisticsSelect.getSupplierid() != null){
            wrapper.eq(Logistics::getSupplierid,logisticsSelect.getSupplierid());
        }

        if (logisticsSelect.getPurchaseid() != null){
            wrapper.eq(Logistics::getPerchaseid,logisticsSelect.getPurchaseid());
        }

        if (logisticsSelect.getFetch() != null){
            wrapper.eq(Logistics::getFetch,logisticsSelect.getFetch());
        }

        if (logisticsSelect.getArrive() != null){
            wrapper.eq(Logistics::getMerchantid,logisticsSelect.getArrive());
        }

        if (logisticsSelect.getWhether() != null){
            wrapper.eq(Logistics::getMerchantid,logisticsSelect.getWhether());
        }

        return list(wrapper);
    }

    /**
     * 物流到达
     *
     * @param logisticsid
     */
    @Override
    public Integer daoda(Integer logisticsid) {

        //判断该物流是否存在
        Logistics logistics = getById(logisticsid);

        if (logistics == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流不存在");
        }

        //修改物流
        logistics.setArrive(1);

        updateById(logistics);

        return 1;
    }

    /**
     * 卖家收货
     *
     * @param merchantS
     */
    @Override
    public Integer shou(MerchantS merchantS) {

        Logistics ss = panshou(merchantS);

        //添加数据
        ss.setSaddr(merchantS.getSaddr());
        ss.setStime(merchantS.getStime());
        ss.setFees(merchantS.getFees());
        ss.setFetch(1); // 设置该物流已经取货

        updateById(ss);

        return 1;
    }



    /**
     * 入仓
     *
     * @param logisticsid
     * @return
     */
    @Override
    @Transactional
    public Integer enter(Integer logisticsid) {

        if (logisticsid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入物流单");
        }

        Logistics logistics = getById(logisticsid);

        if (logistics == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"物流单不存在");
        }

        if (logistics.getFetch() == 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流没有取货");
        }

        if (logistics.getWhether() != 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流已经完成了");
        }

        //修改物流
        logistics.setWhether(1);

        updateById(logistics);

        //调用完成采购
        supplierFeign.ok(logistics.getPerchaseid());

        //调用添加库存
        inventoryFeign.addlist(logistics.getPerchaseid());

        return 1;
    }


    //判断收货的参数校验
    private Logistics panshou(MerchantS merchantS) {

        if (merchantS.getLogisticsid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入物流单");
        }

        if (merchantS.getFees() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入物流费用");
        }

        //判断物流单
        Logistics logistics = getById(merchantS.getLogisticsid());

        if (logistics == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流单不存在");
        }

        if (logistics.getArrive() == 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流还未到达");
        }

        if (logistics.getWhether() == 1){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该物流已经完成了");
        }

        if (merchantS.getSaddr() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入收货地址");
        }

        if (merchantS.getStime() == null){
            merchantS.setStime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        return logistics;
    }


    //判断发货的参数校验
    private void panfa(SupplierF supplierF) {

        if (supplierF.getMerchantid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入收货人");
        }

        if (supplierF.getSupplierid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发货人");
        }

        if (supplierF.getPerchaseid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发货的订购单");
        }

        Logistics one = getOne(new LambdaQueryWrapper<Logistics>().eq(Logistics::getPerchaseid, supplierF.getPerchaseid()));

        if (one != null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该订购单已经发货");
        }

        if (supplierF.getFaddr() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发货地点");
        }

        if (supplierF.getFtime() == null){
            supplierF.setFtime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

    }





}
